package tiltmaze;

import java.util.Arrays;

/**
 * This class represents one cell in a maze.
 * @author Juraj Nižnan
 */
public class Cell {

    /**
     * says whether there is a wall on a certain side of the cell
     * sides are indexed (from 0 to 3) in this order: top, right, bottom, left
     */
    private boolean[] walls = new boolean[4];

    /**
     * says whether there is a ball in this cell
     */
    private boolean start = false;

    /**
     * says whether there is a square in this cell
     */
    private boolean checkpoint = false;


    /**
     * constructor, creates an empty cell with no walls and nothing in it
     */
    public Cell() {
        walls[0] = false;
        walls[1] = false;
        walls[2] = false;
        walls[3] = false;
    }

    /**
     * constructor, creates a cell which walls and content is specified by the
     * given code
     * @param code
     */
    public Cell(int code) {
        setCode(code);
    }

    /**
     * @return an exact copy of the current cell
     */
    public Cell copy(){
        return new Cell(this.getCode());
    }

    /**
     * Builds a wall on the position specified by the given index
     * @param index
     */
    public void buildWall(int index) {
        if (index <0 || index>3) throw new IllegalArgumentException();
        walls[index] = true;
    }

    /**
     * @return true if the cell contains a square
     */
    public boolean getCheckpoint() {
        return checkpoint;
    }

    /**
     * Sets whether the cell contains a square
     * @param checkpoint
     */
    public void setCheckpoint(boolean checkpoint) {
        this.checkpoint = checkpoint;
    }

    /**
     * @return true if the cell contains a ball
     */
    public boolean getStart() {
        return start;
    }

    /**
     * Sets whether the cell contains a ball
     * @param start
     */
    public void setStart(boolean start) {
        this.start = start;
    }

    /**
     * @return an array specifying the walls of the cell
     */
    public boolean[] getWalls() {
        return walls;
    }

    /**
     * Sets the walls of the cell
     * @param walls
     */
    public void setWalls(boolean[] walls) {
        this.walls = walls;
    }

    /**
     * @return numeric code of the cell
     */
    public int getCode() {
        int ret = 0;
        ret += (walls[0]) ? 1 :0;
        ret += (walls[1]) ? 2 :0;
        ret += (walls[2]) ? 4 :0;
        ret += (walls[3]) ? 8 :0;
        ret += (start) ? 16 :0;
        ret += (checkpoint) ? 32 :0;
        return ret;
    }

    public final Boolean setCode(int val){
        if (val>63) return false;
        walls[0] = (val%2 == 1);
        val /= 2;
        walls[1] = (val%2 == 1);
        val /= 2;
        walls[2] = (val%2 == 1);
        val /= 2;
        walls[3] = (val%2 == 1);
        val /= 2;
        start = (val%2 == 1);
        val /= 2;
        checkpoint = (val%2 == 1);
        val /= 2;
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Cell other = (Cell) obj;
        if (!Arrays.equals(this.walls, other.walls)) {
            return false;
        }
        if (this.start != other.start) {
            return false;
        }
        if (this.checkpoint != other.checkpoint) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 13 * hash + Arrays.hashCode(this.walls);
        hash = 13 * hash + (this.start ? 1 : 0);
        hash = 13 * hash + (this.checkpoint ? 1 : 0);
        return hash;
    }
    
}